package com.example.demo.service;
import com.example.demo.entity.GoodsItem;

import com.example.demo.dao.GoodsJPARepository;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.*;
import java.lang.Long;

/**
 *
 */

@Service
public class GoodServer {

    @Autowired
    GoodsJPARepository goodsJPARepository;

    //创建线程安全的Map，模拟Goods的信息存储
    private Map<Long, GoodsItem> goodss = Collections.synchronizedMap(new HashMap<Long, GoodsItem>());

    //添加商品
    public GoodsItem addGoods(GoodsItem goods){
        return goodsJPARepository.saveAndFlush(goods);//saveAndFlush：此命令的更改立即刷新到DB
    }

    //根据id商品
    public GoodsItem getGoods(long id){return goodsJPARepository.getById(id);}

    //根据id更新商品
    public void updateGoods(long id,GoodsItem goods){goodsJPARepository.save(goods);}

    //根据id删除商品
    public void deleteGoods(long id){goodsJPARepository.deleteById(id);}

    //
    public List<GoodsItem> findGoodses(String name, Double price){
      return goodsJPARepository.findByNameAndPrice(name,price);
    }

    public Page<GoodsItem> findGoods(@NotNull Map<String,Object> condition, @NotNull Pageable pageable){
        return goodsJPARepository.findAll((root,query,cb)->{
            List<Predicate> pList = new ArrayList<>();
            if(condition.containsKey("name")){
                pList.add(cb.like(root.get("name"),"%"+condition.get("name")+"%"));
            }
            if (condition.containsKey("price")) {
                pList.add(cb.le(root.get("price"), (Double)condition.get("price")));
            }
            if (condition.containsKey("supplierName")) {
                //根据供应商名称跨表查询，使用root.join('导航属性名').get('关联实体的属性')来表示
                pList.add(cb.equal(root.join("suppliers").get("name"), condition.get("supplierName")));
            }
            return cb.and(pList.toArray(new Predicate[pList.size()]));
        },pageable);
    }
  /*  public GoodsItem getGoods(long id){return goodss.get(id);}*/
    /* public GoodsItem addGoods(GoodsItem goods){
        goodss.put(goods.getId(),goods);
        return goods;
    }*/
   /* public List<GoodsItem> findGoodss(String name,Double price){
        List<GoodsItem> result = new ArrayList<>();
        for(GoodsItem goods: goodss.values()){
            if(name != null && !goods.getName().contains(name)){
                continue;
            }
            if(price != null && !goods.getPrice().equals(price)){
                continue;
            }
            result.add(goods);
        }
        return result;
    }*/

    /*public void updataGoods(long id,GoodsItem goods){
        GoodsItem goods2 = goodss.get(id);
        goods2.setName(goods.getName());
        goods2.setPrice(goods.getPrice());
        goodss.put(id,goods2);
    }*/

   /* public void deleteGood(long id){goodss.remove(id);}*/
}
